package mark.tom.pistols.plugin

import mark.tom.pistols.encryptor.DataEncryptFactory
import mark.tom.pistols.encryptor.EncryptKeyGen
import mark.tom.pistols.encryptor.EncryptKeyGenFactory
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import mark.tom.pistols.encryptor.DataEncrypt
import mark.tom.pistols.encryptor.KeyChainMixer

class SyncAssetsTask extends DefaultTask {

    protected void syncConfigFile(String kind) {
        Project project = getProject()
        // 进入build/intermediates/assets文件夹
        File assetsCatalog = new File(new File(project.buildDir, "intermediates"), "assets")
        if (!assetsCatalog.exists() || !assetsCatalog.isDirectory()) {
            logger.info("暂无创建build/intermediates/assets文件夹")
            return
        }
        // 进入kind对应的文件夹
        def catalog = new File(assetsCatalog, kind)
        // allConfigFiles存放该目录下所有的文件信息
        List<File> allConfigFiles = new ArrayList<File>()
        if (catalog.exists() && catalog.isDirectory()) {
            allConfigFiles.addAll(Arrays.asList(catalog.listFiles()))
        }
        if (allConfigFiles.size() == 0) {
            logger.info("assets中并没有文件")
            return
        }
        // 获取build.gradle中配置的文件名称数据
        Object pistolsConfig = project.extensions.getByName('pistols')
        String[] includes = pistolsConfig.include_files
        if (includes == null || includes.length == 0) {
            logger.info("未配置需要加密的文件名")
            return
        }
        // 加密方法
        String method = pistolsConfig.encrypt_method
        DataEncrypt dataEncrypt = DataEncryptFactory.getDataEncryptByCipher(method)
        if (null == dataEncrypt) {
            logger.info("暂不该加密方法")
            return
        }
        // 加密key
        String encryptKey = pistolsConfig.encrypt_key
        // 解密key
        String decryptKey = pistolsConfig.decrypt_key
        // 如果没有配置密钥则自动生成
        if (null == encryptKey || "" == encryptKey) {
            EncryptKeyGen encryptKeyGen = EncryptKeyGenFactory.getEncryptKeyGenByCipher(method)
            Map<String, String> keyMap = encryptKeyGen.genKey()
            encryptKey = keyMap.get(EncryptKeyGen.PUB_KEY)
            decryptKey = keyMap.get(EncryptKeyGen.PRI_KEY)
        }
        logger.info("====================================")
        logger.info("当前使用加密key:" + encryptKey)
        logger.info("当前使用解密key:" + decryptKey)
        logger.info("====================================")
        dataEncrypt.setPubKey(encryptKey)
        // 额外的参数，如DES的iv
        String extParam = pistolsConfig.ext_param
        dataEncrypt.setExtParam(extParam)
        List<String> encryptFileNames = new ArrayList<String>()
        for (int i = 0; i < allConfigFiles.size(); i++) {
            if (!includes.contains(allConfigFiles.get(i).name)) {
                continue
            }
            String inputData = allConfigFiles.get(i).text
            if (inputData == null || inputData.trim().length() == 0) {
                continue
            }
            try {
                allConfigFiles.get(i).text = dataEncrypt.getEncryptData(inputData)
                encryptFileNames.add(allConfigFiles.get(i).name)
            } catch (Exception ex) {
                ex.printStackTrace()
            }
        }
        if (encryptFileNames.size() == 0) {
            return
        }
        File configInfoFile = new File(catalog, "pistols.jpg")
        if (!configInfoFile.exists()) {
            configInfoFile.createNewFile()
        }
        try {
            configInfoFile.text = KeyChainMixer.getKeyChainInfo(encryptFileNames, decryptKey, method, extParam)
        } catch (Exception ex) {
            ex.printStackTrace()
        }
    }

}
